package com.usefullc.system.service.component.scheduler;

import com.usefullc.common.exception.BizException;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author: Sampson
 * @date: 2022-09-06 11:13
 */
@Component
public class QuartzJobComponent {

    @Autowired
    private Scheduler scheduler;

    @Autowired(required = false)
    private IQuartzHelper quartzHelper;


    public void start(Map<String,String> paramMap)  {
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            add(entry.getKey(),entry.getValue());
        }
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            throw new BizException(e);
        }
    }

    /**
     * 添加，启用
     *
     * @param key
     * @param cronExpression
     */
    public void add(String key, String cronExpression) {
        try {
            JobDetail jobDetail = JobBuilder.newJob(QuartzJob.class)
                    .usingJobData("param", key)
                    .withIdentity(key).build();

            Date startDate = new Date();
            startDate.setTime(startDate.getTime() + 1000);  //延后一秒钟后开始计算执行

            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(key)
                    .withSchedule(scheduleBuilder)
                    .startAt(startDate)
                    .build();
            scheduler.scheduleJob(jobDetail, trigger);
//            if(startNow){
//                scheduler.triggerJob(JobKey.jobKey(key));
//            }
        } catch (SchedulerException e) {
            throw new BizException(e);
        }
    }

    /**
     * 删除，暂停
     *
     * @param key
     */
    public void remove(String key) {
        try {
            //删除之前判断该任务下的模块有没有正在执行...
            if (quartzHelper != null && quartzHelper.isRunning(key)) {
                throw new BizException("不能暂停或删除正在运行的作业");
            }
            scheduler.pauseTrigger(TriggerKey.triggerKey(key));
            scheduler.unscheduleJob(TriggerKey.triggerKey(key));
            scheduler.deleteJob(JobKey.jobKey(key));
        } catch (SchedulerException e) {
            throw new BizException(e);
        }
    }

    public void update(String key, String cronExpression) {
        this.remove(key);
        this.add(key, cronExpression);
    }


    //
//    public void pause(String key) {
//        try {
//            scheduler.pauseTrigger(TriggerKey.triggerKey(key));
//        } catch (SchedulerException e) {
//            throw new BizException(e);
//        }
//    }
//
//    public void resume(String key) {
//        try {
//            scheduler.resumeTrigger(TriggerKey.triggerKey(key));
//        } catch (SchedulerException e) {
//            throw new BizException(e);
//        }
//    }

}
